//
//  StatusCell.swift
//  sinaWeiBo
//
//  Created by smartlei on 15/10/19.
//  Copyright © 2015年 smartlei. All rights reserved.
//

import UIKit

//定义间距
let StatusCellMargin: CGFloat  =  15
//定义头像宽高
let StatusCellIconWH: CGFloat = 40


class StatusCell: UITableViewCell {
    
    //定义VM属性
    var  statusViewModel: StatusViewModel? {
        //监控属性值改变
        didSet {         
            //模型传到top中
            topView.statusViewModel = statusViewModel
            //让pictureView自己计算值
            pictureView.statusViewModel = statusViewModel
            midLable.text = statusViewModel?.status?.text
            
           // print(pictureView.bounds)
            //更新约束:该地方已经sizeToFit，自适应了宽高
            //更新约束后需要自己
            pictureView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                make.width.equalTo(pictureView.bounds.width)
                
              //statusViewModel?.rowheight = CGRectGetMaxY(bottomView.frame)
                
            }
        }
    }
    //提供一个方法让模型数在cell里计算一下返回行高：通过给cell赋值
    //该方法为cell对象调用，
    func rowHeight(model: StatusViewModel) -> CGFloat{
        //1 赋值给cell的属性,则会调用
        statusViewModel = model
        //2 重新layout
        contentView.layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }

    //重构函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //禁止选中cell
        selectionStyle = .None
        //部署cell上的控件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //创建cell的头部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    //中间标签
    lazy var midLable: UILabel = UILabel(labText: "我仅仅cell中间的是测试数据",labTextColor: UIColor.darkGrayColor(),fontSize: 14, textInset:   StatusCellMargin)
    //创建图片视图
    lazy var  pictureView: StatusCellPictureView = StatusCellPictureView()
    //底部视图
    private  lazy var  bottomView: StatusCellBottomView = StatusCellBottomView()
}
//MARK - 创建分类
extension StatusCell {
    
    func setupUI() {
        
      let  speratorView = UIView()
        speratorView.backgroundColor = UIColor.lightGrayColor()
        
        //1 添加控件
        contentView.addSubview(speratorView)
        contentView.addSubview(topView)
        contentView.addSubview(midLable)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        //2 添加约束
        speratorView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(StatusCellMargin)
        }
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(speratorView.snp_top).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(StatusCellMargin + StatusCellIconWH)
            
        }
        
        midLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(topView.snp_left).offset(StatusCellMargin)
            make.right.equalTo(topView.snp_right).offset(-StatusCellMargin)
            
        }
        
//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(midLable.snp_bottom).offset(StatusCellMargin)
//            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
//           
//            //由于要采用更新的方式，所以建约束的时候宽高不要建参考,写个死值
//            make.height.equalTo(150)
//            make.width.equalTo(150)
//        }
    
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(StatusCellIconWH)
      
           // make.bottom.equalTo(contentView.snp_bottom)
        }
    }
}
